﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using Eis51.CampanyCMS.Business;
using Eis51.CampanyCMS.FrameWork;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.Business.Bus;


using Eis51.CampanyCMS.Model;
using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.UI.Admin.ProductsManager
{
    public partial class UpPic : BasePage
    {

        /// <summary>
        /// 绑定数据的源
        /// </summary>
        protected IList ImgData
        {
            get
            {
                if (ViewState["ImgData"] == null)
                {
                    ViewState["ImgData"] = new ArrayList();
                }
                return ViewState["ImgData"] as IList;
            }
            set
            {
                ViewState["ImgData"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                this.ImgData = FileListBLL.GetInstance().GetFilesByProductId("1", FileType.IndexPic);
                Bind();
            }
        }

        private void Bind()
        {
            this.dgNews.Data = this.ImgData;
            this.dgNews.DataBind();
        }


        public void dgNews_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            
            switch (e.CommandName)
            {
                case "Delete":
                    //删除对应的文件
                    FileListBLL.GetInstance().DeleteProductImg(AppContext.ImgUpLoadPath + e.Item.Cells[2].Text, e.Item.Cells[0].Text);
                    for (int i = ImgData.Count; i > 0; i--)
                    {
                        FileList o = ImgData[i - 1] as FileList;
                        if (o.FilePath == e.Item.Cells[2].Text)
                        {
                            ImgData.Remove(o);
                        }
                    }
                    Bind();
                    break;
                default:
                    break;
            }
        }



        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string sss = UploadFile.PostedFile.FileName;
            FileInfo info = new FileInfo(sss);
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + info.Extension;
            this.UploadFile.PostedFile.SaveAs(AppContext.ImgUpLoadPath + fileName);
            FileList fileList = new FileList();
            fileList.KeyType = ((int)FileType.IndexPic).ToString();
            fileList.KeyValue =1;
            fileList.FilePath = fileName;
            fileList.FileName = fileName;
            FileListBLL.GetInstance().Insert(fileList);
            this.ImgData.Add(fileList);
            Bind();
            CreateHtml();
        }

        private void CreateHtml()
        {


            IList itImgs = FileListBLL.GetInstance().GetFilesByProductId("1", FileType.IndexPic);
            string strImg = "  <img src='{0}' alt='Photo' /> ";
            string PicList = ""; 
            foreach (FileList fl in itImgs)
            {
                string ImgUrl = AppContext.ImgUpLoadShowPath + fl.FileName;
                ImgUrl = ImgUrl.Replace("///", "/");
                PicList += string.Format(strImg, ImgUrl);
            }
            WirteHtml(PicList);
        }


        public  void WirteHtml(string Piclist)
        {
            string strHtml = HtmlUitl.GetHTML(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "js/TempIndex.html");
            string Path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "js/ScorllIndex.html";
            ; 
            if (File.Exists(Path))
            {
                File.Delete(Path);
            }
            strHtml = strHtml.Replace("|PIC|", Piclist);
            HtmlUitl.CreaetHTMLFile(Path, strHtml);
        }

        protected void dgNews_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem
                || e.Item.ItemType == ListItemType.Item)
            {
                System.Web.UI.WebControls.Image img = e.Item.FindControl("img") as System.Web.UI.WebControls.Image;
                img.ImageUrl = AppContext.ImgUpLoadShowPath + e.Item.Cells[2].Text;
            }
        }

        protected void bntCreate_Click(object sender, EventArgs e)
        {
            CreateHtml();
        }
    }
}
